import {PageContainer} from "@ant-design/pro-components";
import AutoReplyTable from "@/pages/AutoReply/components/AutoReplyTable";
import {AutoReplyVO, saveAutoReply, updateAutoReply} from "@/services/AutoReplyService";
import AutoReplyUpdateModal from "@/pages/AutoReply/components/AutoReplyUpdateModal";
import {useRef, useState} from "react";
import {message} from "antd";
import {ActionType} from "@ant-design/pro-table";
import {useSnapshot} from "@@/exports";
import {AutoReplyModel} from "@/models/autoReply";


const AutoReply = () => {
    // 表格引用
    const tableRef = useRef<ActionType>()
    let autoReplyState = useSnapshot(AutoReplyModel.state);

    // 提交修改
    const onUpdateFinish = async (formData: any) => {
        let dto: AutoReplyVO = JSON.parse(JSON.stringify(formData))
        // 字段处理

        let func = autoReplyState.autoReply ? updateAutoReply : saveAutoReply
        if (await func(dto)) {
            message.success("操作成功");
            // 刷新表格
            tableRef.current?.reload();
            return true;
        }
        return false;
    }

    return <PageContainer title="自动回复">
        {/*自动回复表格 开始*/}
        <AutoReplyTable tableRef={tableRef} />
        {/*自动回复表格 结束*/}

        {/*修改模态框 开始*/}
        <AutoReplyUpdateModal onFinish={onUpdateFinish}/>
        {/*修改模态框 结束*/}
    </PageContainer>
}

export default AutoReply
